<?php

namespace app\index\controller;
use app\admin\model\Question as QuestionModel;
use app\admin\model\User;
use app\index\model\Collection;
use app\index\model\Coment;
use think\Image;
use think\Request;
use think\Session;

class Question extends Common{
    protected $request;
    protected $question;
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->request=Request::instance();
        $this->question=new QuestionModel();
    }
    public function article(){
        if(!Session::has("user_id")==false){
            $user_id=Session::get('user_id');
        }else{
            $user_id='';
        }


        $user=new User();
        $user1=$user->getOne(['user_id'=>$user_id]);
        $questions=$this->question->getAuu(['q_del'=>0,'audit'=>1]);
        $this->assign('user',$user1);
        $coll_ques=$this->question->getThree(['audit'=>1]);
        $this->assign("coll_ques",$coll_ques);
        $this->assign('questions',$questions);
        return $this->fetch();
    }
    public function add(){
        $user_id = input('user_id', '');
        if($this->request->isGet()){
            if (!$user_id) {
                $this->error('请先登录', 'login/index');
            }
            $user=new User();
            $user1=$user->getOne(['user_id'=>$user_id]);
            $this->assign('user',$user1);
            $this->assign('user_id',$user_id);
            return $this->fetch();
        }else{
            $data=$this->request->post();
            $data['create_time']=time();
            $res=$this->question->addData($data);
            if($res){
                $this->success('发布文章成功','question/article');

            }else{
                $this->error('发布文章失败','question/add');
            }
        }

    }
    public function edite(){
        $user_id = input('user_id', '');
        $q_id=input('q_id','');
        if($this->request->isGet()){
            if (!$user_id) {
                $this->error('请先登录', 'login/index');
            }
            $user=new User();
            $user1=$user->getOne(['user_id'=>$user_id]);
            $question=$this->question->getOne(['q_id'=>$q_id,'user_id'=>$user_id]);
            $this->assign('question',$question);
            $this->assign('user',$user1);
            $this->assign('user_id',$user_id);
            return $this->fetch();
        }else{

            $data=$this->request->post();
            $data['update_time']=time();
            $res=$this->question->saveData(['q_id'=>$q_id,'user_id'=>$user_id],$data);
            if($res){
                $this->success('修改文章成功','user/collection');

            }else{
                $this->error('修改文章失败',"edite?q_id=$q_id&user_id=$user_id");
            }
        }

    }
    public function qDel(){
        $user_id = input('user_id', '');
        $q_id=input('q_id','');
        $res=$this->question->saveData(['q_id'=>$q_id,'user_id'=>$user_id],['q_del'=>1]);
        if($res){
            return json([
                'status'=>1,
                'msg' => '删除成功'
            ]);
        }else{
            return json([
                'status'=>0,
                'msg' => '删除失败'
            ]);
        }
    }
    public function question(){

        $user_id = input('user_id', '');
        if (!$user_id) {
            $this->error('请先登录', 'login/index');
        }
        $user=new User();
        $q_id=input('q_id');
        $question=$this->question->getOne(['q_id'=>$q_id]);
        $coment=$user->pageCom(['question_id'=>$q_id,'c_del'=>0,'zhiding'=>0]);
        $conllection=new Collection();
        $cltn=$conllection->pageCltn();
        $this->assign('cltn',$cltn);
        $user1=$user->getOne(['user_id'=>$user_id]);
        $this->assign('user',$user1);
        $this->assign('coment',$coment);
        $this->assign('question',$question);
        $this->assign('q_id',$q_id);
        $comment=new Coment();
        $comc=$user->getFocus(['question_id'=>$q_id,'zhiding'=>1,'c_del'=>0]);
        $this->assign('comc',$comc);
        $coll_ques=$this->question->getThree(['audit'=>1]);
        $this->assign("coll_ques",$coll_ques);
        $click=$question['click']+1;
        $this->assign('user_id',$user_id);
        $a=$this->question->saveData(['q_id'=>$q_id],['click'=>$click]);
        return $this->fetch();
    }
    public function coment(){
        $user_id=input('user_id');
        $q_id=input('q_id');
        $comment=input('comment');
        $coment=new Coment();
        $create_time=time();
        $data=[
            'user_id'=>$user_id,
            'question_id'=>$q_id,
            'title'=>$comment,
            'create_time'=>$create_time
        ];
        $user=new User();
        $user1=$user->getOne(['user_id'=>$user_id]);
        $name=$user1['user_name'];
        $image_thumb=$user1['image_thumb'];
        $res=$coment->addData($data);
        $create_time=date("Y-m-d H:i:s",$create_time);
        if($res){
            return json([
                'status'=>1,
                'msg' => '评论成功',
                'name'=>$name,
                'image_thumb'=>$image_thumb,
                'comment'=>$comment,
                'create_time'=>$create_time,
                'dz'=>0
            ]);
        }else{
            return json([
                'status'=>0,
                'msg' => '评论失败'
            ]);
        }
    }
    public function dzdz(){
        $q_id=input('q_id');
        $c_id=input('c_id');
        $dz=input('dz');
        $data=[
            'dz'=>$dz+1
        ];
        $coment=new Coment();
        $res=$coment->saveData(['c_id'=>$c_id],$data);
        if($res){
            return json([
                'status'=>1,
                'msg' => '点赞成功',
                'dz'=>$dz+1
            ]);
        }else{
            return json([
                'status'=>0,
                'msg' => '点赞失败'
            ]);
        }

    }
    public function cDel(){
        $c_id=input('c_id');
        $coment=new Coment();

        $res=$coment->saveData(['c_id'=>$c_id],['c_del'=>1]);
        if($res){
            return json([
                'status'=>1,
                'msg' => '删除成功'
            ]);
        }else{
            return json([
                'status'=>0,
                'msg' => '删除失败'
            ]);
        }
    }
    public function scsc(){
        $user_id=input('user_id');
        $q_id=input('question_id');
        $data=[
            'user_id'=>$user_id,
            'question_id'=>$q_id
        ];
        $collection=new Collection();
        $ccc=$collection->getOne($data);
        if(!empty($ccc)){
            return json([
                'status'=>0,
                'msg' => '不能重复收藏同一篇文章'
            ]);
        }
        $res=$collection->addData($data);
        if($res){
            return json([
                'status'=>1,
                'msg' => '收藏成功'
            ]);
        }else{
            return json([
                'status'=>0,
                'msg' => '收藏失败'
            ]);
        }
    }
    public function uploadImg()
    {

        $file = $this->request->file('file_data');
        //原图保存路径
        $path = ROOT_PATH . 'public' . DS . 'uploads' . DS . 'question';
        $info = $file->move($path);
        //缩略图名称
        $thumb_name = 'thumb_' . $info->getFilename();
        //缩略图保存路径和文件名称
        $thumb_path = ROOT_PATH . 'public' . DS . 'uploads' . DS . 'question' . DS . 'thumb' . DS . $thumb_name;
        if ($info) {
            //缩略图
            $image = Image::open($path.DS.$info->getSaveName());
            $image->thumb(200, 200, Image::THUMB_CENTER)->save($thumb_path);
            return json([
                'status' => 1,
                'path' => $info->getSaveName(),
                'thumb' => 'thumb' . DS . $thumb_name
            ]);
        } else {
            return json([
                'status' => 0,
                'err' => $file->getError()
            ]);
        }
    }
}